package com.kunlun.basedata.service.impl;

import com.kunlun.basedata.annotation.OperatorLogger;
import com.kunlun.basedata.model.ShiroConfigModel;
import com.kunlun.basedata.model.SignTokenModel;
import com.kunlun.basedata.model.TokenModel;
import com.kunlun.basedata.model.UserModel;
import com.kunlun.basedata.model.enums.OperatorLogType;
import com.kunlun.basedata.service.IBasedataService;
import com.kunlun.basedata.service.IOnlineUserService;
import com.kunlun.basedata.service.IShiroService;
import com.kunlun.basedata.service.IUserService;
import com.kunlun.basedata.utils.CommonUtil;
import com.kunlun.basedata.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

@Service
@Transactional
@Primary
public class ShiroServiceImpl implements IShiroService {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private ShiroConfigModel shiroConfig;

    @Autowired
    private IOnlineUserService onlineUserService;

    @Autowired
    private IUserService userService;
    @Autowired
    @Qualifier("redisService")
    private RedisJedisService jedisService;


    @OperatorLogger(type = OperatorLogType.LOGIN, description = "用户登录系统")
    @Override
    public TokenModel handleLogin(HttpServletRequest request,String userName, String password) throws Exception {
        // 处理在线用户
        String id = CommonUtil.generateUUID();
        Date loginDate = new Date();
        onlineUserService.addOnlineUser(request,id, userName, loginDate);

        // 登录用户基本信息
        UserModel userByUserName = userService.getUserByUserName(userName);
        UserModel userModel = new UserModel();
        userModel.setId(userByUserName.getId());
        userModel.setUserName(userByUserName.getUserName());
        userModel.setPassword(userByUserName.getPassword());
        userModel.setPhoneNumber(userByUserName.getPhoneNumber());
        userModel.setEmail(userByUserName.getEmail());
        userModel.setCreateTime(userByUserName.getCreateTime());
        userModel.setModifiedTime(userByUserName.getModifiedTime());

        // 生成Token
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String loginTime = format.format(loginDate);
        SignTokenModel signToken = new SignTokenModel(id, userName, password, loginTime, shiroConfig.getSecret(), shiroConfig.getExpireTime());
        String shiroToken = JwtTokenUtil.sign(signToken);
        TokenModel tokenModel = new TokenModel(shiroToken, userModel);

        // 缓存token到redis
        String redisKey = userName + "_" + loginTime;
        jedisService.set(redisKey, shiroToken, shiroConfig.getExpireTime(), 1);
        return tokenModel;
    }

    @OperatorLogger(type = OperatorLogType.LOGIN, description = "用户注销登录")
    @Override
    public void handleLogout(HttpServletRequest request, String userName) throws Exception {
        String token = request.getHeader(shiroConfig.getTokenHeader());
        String onlineUserId = JwtTokenUtil.getTokenInfo(token, "onlineUserId");
        String loginTime = JwtTokenUtil.getTokenInfo(token, "loginTime");

        // 处理在线用户
        onlineUserService.updateOnlineUser(onlineUserId);

        // 注销成功删除缓存在redis的Token
        String redisKey = userName + "_" + loginTime;
        jedisService.del(redisKey, 1);
    }
}
